package com.sz.biz.logistics.core.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.FreightItem;
import com.sz.biz.logistics.core.service.PriceQuotationItemService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Package: com.sz.biz.logistics.core.service.impl <br>
 * Function: PriceQuotationItemServiceImpl <br>
 * Author: wentao.chang. <br>
 * Date: 2017-03-20 15:00:00.
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PriceQuotationItemServiceImpl extends AbstractService implements PriceQuotationItemService {
    @Override
    protected String getMapperNamespace() {
        return "PriceFreightItemMapper";
    }

    @Override
    public int save(FreightItem entry) {
        dao.save(getSqlName("insert"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_QUOTATION_ITEM", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_ITEM, UserActions.ADD, "添加价格明细", dataBackup);
        return entry.getId();
    }

    @Override
    public void batchsave(List<FreightItem> list) {
        dao.batchInsert(getSqlName("insert"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_QUOTATION_ITEM", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_ITEM, UserActions.ADD, "添加价格明细", dataBackup);
    }

    @Override
    public List<FreightItem> findByQuotationId(int quotationId) {
        ParamData pd = new ParamData();
        pd.put("quotationId", quotationId);
        return dao.findForList(getSqlName("findByQuotationId"), pd, FreightItem.class);
    }

    /**
     * 根据quotationSetId查询单个实体
     *
     * @param quotationSetId
     * @return 查询出来的实体对象
     */
    @Override
    public List<FreightItem> findByQuotationSetId(int quotationSetId) {
        ParamData pd = new ParamData();
        pd.put("quotationSetId", quotationSetId);
        return dao.findForList(getSqlName("findByQuotationSetId"), pd, FreightItem.class);
    }

    @Override
    public FreightItem findById(Integer id) {
        ParamData pd = new ParamData();
        pd.put("id", id);
        FreightItem entry = (FreightItem) dao.findForObject(getSqlName("selectByPrimaryKey"), pd);
        return entry;
    }

    /**
     * 根据quotationId删除起点，用于价格下线编辑价格时直接删除
     *
     * @param quotationId quotationId
     */
    @Override
    public void deletedByQuotationId(Integer quotationId) {
        dao.delete(getSqlName("deletedByQuotationId"), quotationId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_QUOTATION_ITEM", quotationId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_ITEM, UserActions.DELETE, "删除价格明细", dataBackup);
    }

    /**
     * 根据quotationSetId删除起点，用于价格下线编辑价格时直接删除
     *
     * @param quotationSetId quotationSetId
     */
    @Override
    public void deletedByQuotationSetId(Integer quotationSetId) {
        dao.delete(getSqlName("deletedByQuotationSetId"), quotationSetId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_QUOTATION_ITEM", quotationSetId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION_ITEM, UserActions.DELETE, "删除价格明细", dataBackup);
    }


    /**
     * 根据quotationId+toRegionUid获取价格方案下某个目标分区的报价
     *
     * @param quotationSetId quotationId
     * @param toRegionUid    toRegionUid
     */
    @Override
    public List<FreightItem> findByQuotationSetIdAndRegionUid(int quotationSetId, String toRegionUid) {
        ParamData pd = new ParamData();
        pd.put("quotationSetId", quotationSetId);
        pd.put("toRegionUid", toRegionUid);
        return dao.findForList(getSqlName("findByQuotationSetIdAndRegionUid"), pd, FreightItem.class);
    }
}
